#include "INPUT.H"

int getKey(){
    int key, lo, hi;
    key = bioskey(0);
    lo = key & 0x00FF;
    hi = (key & 0xFF00) >> 8;
    return (lo == 0) ? hi + 256 : lo;
}
void mouseInterrupt(word mode, union REGS *regs){
	regs->x.ax = mode;
	int86(0x33, regs, regs);
}
boolean initMouse(byte *numButtons){
	word mouseOn;
	union REGS regs;
	mouseInterrupt(0x00, &regs);
	
	mouseOn = regs.x.ax;
	*numButtons = regs.x.bx;
	return (mouseOn==0xFFFF);
}
void showMouseCursor(){
	union REGS regs;
	mouseInterrupt(0x01, &regs);
}
void hideMouseCursor(){
	union REGS regs;
	mouseInterrupt(0x02, &regs);
}
MouseState getMouseState(){
	MouseState state;
	union REGS regs;
	mouseInterrupt(0x03, &regs);
	
	state.x = (regs.x.cx>>1);
	state.y = regs.x.dx;
	state.buttonState = regs.h.bl;
	return state;
}
void setMouseState(MouseState state){
	union REGS regs;
	regs.x.cx = (state.x<<1);
	regs.x.dx = state.y;
	mouseInterrupt(0x04, &regs);
}
short getMousePress(byte button, MouseState *state){
	union REGS regs;
	regs.x.bx = button;
	mouseInterrupt(0x05, &regs);
	
	state->x = (regs.x.cx>>1);
	state->y = regs.x.dx;
	state->buttonState = regs.x.ax;
	return regs.x.bx;
}
short getMouseRelease(byte button, MouseState *state){
	union REGS regs;
	regs.x.bx = button;
	mouseInterrupt(0x06, &regs);
	
	state->x = (regs.x.cx>>1);
	state->y = regs.x.dx;
	state->buttonState = regs.x.ax;
	return regs.x.bx;
}
void boundMouseHorizontal(word min, word max){
	union REGS regs;
	regs.x.cx = min;
	regs.x.dx = max;
	mouseInterrupt(0x07, &regs);
}
void boundMouseVertical(word min, word max){
	union REGS regs;
	regs.x.cx = min;
	regs.x.dx = max;
	mouseInterrupt(0x08, &regs);
}
void getMouseMotion(short *x, short *y){
	union REGS regs;
	mouseInterrupt(0x0B, &regs);
	*x = regs.x.cx;
	*y = regs.x.dx;
}
void boundMouse(word left, word top, word right, word bottom){
	union REGS regs;
	regs.x.cx = right;
	regs.x.dx = bottom;
	regs.x.si = left;
	regs.x.di = top;
	mouseInterrupt(0x10, &regs);
}
